﻿using HMACAuthentication.WebApi.Filters;
using YMJF_Service.Common;
using YMJF_Service.Models;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace YMJF_Service.Controllers
{
    /// <summary>
    /// 页面元素
    /// </summary>
    [RoutePrefix("api/html")]

    public class HtmlElementController : ApiController
    {
        ConnectionStringMySQLDB db = new ConnectionStringMySQLDB();

        /// <summary>
        /// 获取某页面元素组
        /// </summary>
        /// <param name="fkey">页面key</param>
        /// <returns></returns>
        [HttpGet]
        [Route("obj")]
        public ApiResult getHtmlElements(string fkey)
        {
            ApiResult res = new ApiResult();
            try
            {

                List<HtmlElement> list = db.Fetch<HtmlElement>("where HtmlFKey=@0 and SysDeleted=0 ",fkey);
                Dictionary<string, string> Htmls = list.GroupBy(x => x.HtmlKey).ToDictionary(x => x.First().HtmlKey, x => x.First().HtmlContent);
                //string json = (new JavaScriptSerializer()).Serialize(Htmls);
                res.code = HttpCode.success;
                res.message = "获取成功";
                res.data = Htmls;
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "html", "获取某页面元素组：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }


        /// <summary>
        /// 获取某页面下元素列表
        /// </summary>
        /// <param name="fkey">所属页面key</param>
        /// <param name="page">页数</param>
        /// <param name="pagesize">每页行数</param>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        [HMACAuthenticationAttribute]
        public ApiResult GetList(string fkey, long page, long pagesize)
        {
            ApiResult res = new ApiResult();
            try
            {

                Page<HtmlElement> list = HtmlElement.Page(page, pagesize, "where HtmlFKey=@0 and SysDeleted=0 order by SysOrder", fkey);
                    res.code = HttpCode.success;
                    res.message = "获取列表成功";
                    res.data = list;
               
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "html", "获取某页面下元素列表错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }


        /// <summary>
        /// 获取页面元素详细
        /// </summary>
        /// <param name="Id">元素ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("get")]
        [HMACAuthenticationAttribute]
        public ApiResult GetHtmlElement(string Id)
        {
            ApiResult res = new ApiResult();
            try
            {

                HtmlElement data = HtmlElement.SingleOrDefault(" where SysDeleted = 0 and ID = @0 ", Id);
                if (data != null)
                {
                    res.code = HttpCode.success;
                    res.message = "获取页面元素详细成功";
                    res.data = data;
                }
                else
                {
                    res.code = HttpCode.Que_Failed;
                    res.message = "获取页面元素详细失败";
                }

            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "html", "获取页面元素详细错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 添加页面元素
        /// </summary>
        /// <param name="data">页面元素信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("add")]
        [HMACAuthenticationAttribute]
        public ApiResult AddHtmlElement(HtmlElement data)
        {
            ApiResult res = new ApiResult();

            try
            {
                data.ID = Guid.NewGuid().ToString();
                data.SysCreate = DateTime.Now;
                data.SysLastUpd = DateTime.Now;
                data.SysDeleted = false;
                object obj = data.Insert();
                if (obj != null)
                {
                    res.code = HttpCode.success;
                    res.message = "添加成功";
                    res.data = data;
                }
                else
                {
                    res.code = HttpCode.Add_Failed;
                    res.message = "添加失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "html", "添加页面元素错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

       
        /// <summary>
        /// 修改页面元素
        /// </summary>
        /// <param name="data">文章信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("update")]
        [HMACAuthenticationAttribute]
        public ApiResult UpdateHtmlElement(HtmlElement data)
        {
            ApiResult res = new ApiResult();

            try
            {
                HtmlElement data2 = HtmlElement.SingleOrDefault(" where ID = @0 ", data.ID);
                data2.HtmlKey = data.HtmlKey;
                data2.HtmlFKey = data.HtmlFKey;
                data2.HtmlName = data.HtmlName;
                data2.HtmlContent = data.HtmlContent;
                data2.Type = data.Type;
                data2.SysOrder = data.SysOrder;
                data2.SysLastUpd = DateTime.Now;
                int obj = data2.Update();
                if (obj > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "修改成功";
                }
                else
                {
                    res.code = HttpCode.Mod_Failed;
                    res.message = "修改失败";
                }

            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "html", "修改页面元素错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }


        /// <summary>
        ///  删除页面元素
        /// </summary>
        /// <param name="ID">页面元素ID</param>
        /// <returns></returns>
        [HttpPost]
        [Route("del")]
        [HMACAuthenticationAttribute]
        public ApiResult DelHtmlElement(string ID)
        {
            ApiResult res = new ApiResult();

            try
            {
                int obj = HtmlElement.Update(string.Format(" set SysDeleted = 1,SysLastUpd = '{1}' where ID = '{0}' ", ID, DateTime.Now));
                if (obj > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "删除成功";
                }
                else
                {
                    res.code = HttpCode.Del_Failed;
                    res.message = "删除失败";
                }

            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "html", "删除页面元素错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }


    }
}
